package com.rabbiter.pm.service.impl;

import cn.dev33.satoken.util.SaFoxUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.pm.domain.Car;
import com.rabbiter.pm.domain.CarToUser;
import com.rabbiter.pm.domain.User;
import com.rabbiter.pm.domain.vo.CarInitiateVo;
import com.rabbiter.pm.domain.vo.CarInitiateingVo;
import com.rabbiter.pm.domain.vo.CarVo;
import com.rabbiter.pm.mapper.CarMapper;
import com.rabbiter.pm.mapper.CarUserMapper;
import com.rabbiter.pm.mapper.UserMapper;
import com.rabbiter.pm.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rabbiter
 * @since 2023-03-27
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Resource
    private CarMapper carMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private CarUserMapper carUserMapper;



    @Override
    public Object getList(CarVo carVo) {
        Page<Car> page = new Page<>(carVo.getPageNum(),carVo.getPageSize());
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        if(!SaFoxUtil.isEmpty(carVo.getPerson())){
            wrapper.like("person",carVo.getPerson());
        }
        if(!SaFoxUtil.isEmpty(carVo.getCard())){
            wrapper.like("card",carVo.getCard());
        }
        if(!SaFoxUtil.isEmpty(carVo.getCarType())){
            wrapper.like("car_type",carVo.getCarType());
        }

        return carMapper.selectPage(page,wrapper);
    }

    @Override
    public Object addCar(Car car) {
        Map<String,Object> res = new HashMap<>();
        Car car1 = carMapper.selectOne(new QueryWrapper<Car>().eq("card",car.getCard()));
        if(SaFoxUtil.isEmpty(car1)){
            car.setXtime(LocalDateTime.now());
            if (car.getCarImage()==null){
                car.setCarImage("https://img-blog.csdnimg.cn/20210327163410707.png");
            }
            int cou = carMapper.insert(car);
            if(cou>0){
                res.put("flag",true);
                res.put("msg","添加成功");
            }else{
                res.put("flag",false);
                res.put("msg","添加失败，请重新尝试");
            }
        }else{
            res.put("flag",false);
            res.put("msg","已存在车牌信息");
        }
        return res;
    }

    /**
     * 发起审批申请车俩
     * 用户发起审批，传入用户名，和车辆车牌，车辆id
     * 通过车辆id，获取当前车辆信息，并且更改approvaling字段，填入person字段
     */
    @Override
    public Object Initiate(CarInitiateVo carInitiateVo) {
        Map<String,Object> res = new HashMap<>();
        //查询传入用户是否存在
        User user = userMapper.selectById(carInitiateVo.getUid());
        if (SaFoxUtil.isEmpty(user)){
            res.put("flag",false);
            res.put("msg","用户不存在");
            return res;
        }
        //查询传入车辆是否存在
        Car car = carMapper.selectById(carInitiateVo.getTid());
        if (SaFoxUtil.isEmpty(car)){
            res.put("flag",false);
            res.put("msg","车辆不存在");
            return res;
        }
        //判断车辆是否已经审批通过
        if (!SaFoxUtil.isEmpty(car.getApproval())){
            res.put("flag",false);
            res.put("msg","车辆已经审批通过");
            return res;
        }

        //判断车辆是否已经发起审批
        if (!SaFoxUtil.isEmpty(car.getApprovaling())){
            res.put("flag",false);
            res.put("msg","车辆已经发起审批");
            return res;
        }
        //更改车辆信息
        car.setPerson(user.getUsername());
        car.setApprovaling("1");
        car.setXtime(LocalDateTime.now());
        int cou = carMapper.updateById(car);
        if(cou>0){
            res.put("flag",true);
            res.put("msg","发起审批成功");
        }else{
            res.put("flag",false);
            res.put("msg","发起审批失败，请重新尝试");
        }
        return res;
    }

    /**
     * 审核提交过来的申请
     * @param
     * @return
     */
    @Override
    public Object approval(CarInitiateingVo carInitiateingVo) {
        // 通过tid查询车辆信息
        Car car = carMapper.selectById(carInitiateingVo.getTid());
        Map<String,Object> res = new HashMap<>();
        if (SaFoxUtil.isEmpty(car)){
            res.put("flag",false);
            res.put("msg","车辆不存在");
            return res;
        }
        // 把审批状态改变（成功）
        if (carInitiateingVo.getApproval().equals("1")){
            CarToUser carToUser = new CarToUser();
            carToUser.setTid(carInitiateingVo.getTid());
            carToUser.setUid(carInitiateingVo.getUid());
            carToUser.setXtime(LocalDateTime.now());
            carUserMapper.insert(carToUser);
            car.setApproval("1");
            car.setApprovaling("2");
            carMapper.updateById(car);
        }
        //失败
        if (carInitiateingVo.getApproval().equals("0")){
            car.setApproval("0");
            car.setApprovaling("2");
        }
        car.setXtime(LocalDateTime.now());
        int cou = carMapper.updateById(car);
        if(cou>0){
            res.put("flag",true);
            res.put("msg","审批成功");
        }else{
            res.put("flag",false);
            res.put("msg","审批失败，请重新尝试");
        }
        return res;
    }

    @Override
    public Object deleteCar(Integer tid) {
        // 通过tid查询车辆信息
        Car car = carMapper.selectById(tid);
        Map<String,Object> res = new HashMap<>();
        if (SaFoxUtil.isEmpty(car)){
            res.put("flag",false);
            res.put("msg","车辆不存在");
            return res;
        }
        int cou = carMapper.deleteById(tid);
        if(cou>0){
            res.put("flag",true);
            res.put("msg","删除成功");
        }else{
            res.put("flag",false);
            res.put("msg","删除失败，请重新尝试");
        }
        return res;
    }
}
